package com.jic.interaction.JDVOP.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jic.interaction.JDVOP.common.utils.JdVopHttpUtil;
import com.jic.interaction.JDVOP.common.utils.RestResult;
import com.jic.interaction.JDVOP.mapper.MProductPriceMapper;
import com.jic.interaction.JDVOP.mapper.ProductMapper;
import com.jic.interaction.JDVOP.mapper.ProductPriceMapper;
import com.jic.interaction.JDVOP.model.entity.ProductPrice;
import com.jic.interaction.JDVOP.model.vo.request.GoodsSoldRequest;
import com.jic.interaction.JDVOP.model.vo.response.JDProductPrice;
import com.jic.interaction.JDVOP.service.GoodsSoldService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @description: TODO
 * @author: lixuehui
 * @date: 2020/11/15 16:39
 * @Version 1.0
 */
@Service
@Slf4j
public class GoodsSoldServiceImpl implements GoodsSoldService {

    @Value("${route.JDVOP}")
    private String url;


    @Resource
    private ProductPriceMapper productPriceMapper;

    @Override
    public RestResult getSellPrice(GoodsSoldRequest request) throws Exception {
        log.info("查询在客户商品池中的商品价格，入参 = {}", JSON.toJSONString(request));
        String url = this.url +"api/price/getSellPrice";
        String str = JdVopHttpUtil.doRequestPost(url, JSON.parseObject(JSON.toJSONString(request), Map.class));
        log.info("查询返回结果，入参 = {}", str);

        Map m = JSONObject.parseObject(str);
        JSONObject value = JSONObject.parseObject(str);
        if (m.get("success").toString() != "true") {
            return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());
        }
        List<JDProductPrice> list = null;
        if (value.containsKey("result")) {
            // 获取到key为shoppingCartItemList的值
            if (value.getJSONArray("result")!=null){
                list = value.getJSONArray("result").toJavaList(JDProductPrice.class);
            }
        }
        ProductPrice productPrice = new ProductPrice();
        for (int i = 0; i < list.size(); i++){
            productPrice.setProductId(list.get(i).getSkuId());
            productPrice.setMarketPrice(list.get(i).getPrice());
            productPrice.setGroupBuyingPrice(list.get(i).getPrice());
            productPrice.setRecommendRetailPrice(list.get(i).getPrice());
        }
        productPriceMapper.insert(productPrice);
        return RestResult.success(m.get("result").toString(), m.get("resultCode").toString(), m.get("resultMessage").toString());
    }
}
